﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Silvermoon.Shapes;
using Silvermoon.OpenGL;
using Silvermoon.Classes;
using Silvermoon.Windows;

namespace Silvermoon.Controls
{
    public class CheckItem : ContainerItem
    {
        public static readonly Command ToggleSelectionCommand = new Command();

        private bool fontSet;
        private CheckBox checkBox;
        private StringShape textShape;

        public CheckItem()
            : base()
        {
            checkBox = new CheckBox { Margin = new Margin { Left = 2, VerticalCentered = true }, Size = new Size(64, 64), TextColor = PhoneColors.White };
            checkBox.CheckedChanged += new EventHandler(checkBox_CheckedChanged);
            textShape = new StringShape { LineAlignment = Alignment.Center };
            Control = checkBox;
            Shapes.AddFirst(textShape);
            Height = 80;
        }

        void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            IsSelected = checkBox.IsChecked;
            RaiseCommand(ToggleSelectionCommand);
        }

        protected override void OnSelectionChanged(bool animated)
        {
            base.OnSelectionChanged(animated);
            checkBox.IsChecked = IsSelected;
        }

        public string Text
        {
            get { return textShape.Text; }
            set
            {
                textShape.Text = value;
                Invalidate(InvalidationFlag.Text);
            }
        }

        public FontSprite Font
        {
            get { return textShape.Font; }
            set
            {
                fontSet = true;
                textShape.Font = value;
                Invalidate(InvalidationFlag.Text);
            }
        }

        protected override void OnLayout()
        {
            base.OnLayout();
            if (CheckFlag(InvalidationFlag.Layout | InvalidationFlag.Size))
            {
                int left = checkBox.Right + 8;
                textShape.Bounds = new Rectangle(left, 0, Width - left, Height);
            }
        }

        protected override void ApplyStyle()
        {
            base.ApplyStyle();
            if (!fontSet) Font = SystemFonts.Medium;
        }

    }
}
